package com.team.childapp.util

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager

/**
 * Create on: 2025/1/2
 * by Mr.Zhu
 * Description: TODO
 */
class ShareDataUtils(private val context: Context){
    private val sharedPreferences: SharedPreferences by lazy {
        context.getSharedPreferences("child_app_prefs", Context.MODE_PRIVATE)
    }

    // 存储字符串数据
    fun putString(key: String, value: String) {
        sharedPreferences.edit().putString(key, value).apply()
    }

    // 读取字符串数据
    fun getString(key: String, defaultValue: String = ""): String {
        return sharedPreferences.getString(key, defaultValue) ?: defaultValue
    }

    // 存储布尔数据
    fun putBoolean(key: String, value: Boolean) {
        sharedPreferences.edit().putBoolean(key, value).apply()
    }

    // 读取布尔数据
    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return sharedPreferences.getBoolean(key, defaultValue)
    }

    // 存储整数数据
    fun putInt(key: String, value: Int) {
        sharedPreferences.edit().putInt(key, value).apply()
    }

    // 读取整数数据
    fun getInt(key: String, defaultValue: Int = 0): Int {
        return sharedPreferences.getInt(key, defaultValue)
    }

    // 存储长整型数据
    fun putLong(key: String, value: Long) {
        sharedPreferences.edit().putLong(key, value).apply()
    }

    // 读取长整型数据
    fun getLong(key: String, defaultValue: Long = 0L): Long {
        return sharedPreferences.getLong(key, defaultValue)
    }

    // 存储浮点型数据
    fun putFloat(key: String, value: Float) {
        sharedPreferences.edit().putFloat(key, value).apply()
    }

    // 读取浮点型数据
    fun getFloat(key: String, defaultValue: Float = 0f): Float {
        return sharedPreferences.getFloat(key, defaultValue)
    }

    // 删除指定键的数据
    fun remove(key: String) {
        sharedPreferences.edit().remove(key).apply()
    }

    // 清除所有数据
    fun clear() {
        sharedPreferences.edit().clear().apply()
    }
}